//
//  NavigationController.swift
//  SwiftDemo
//
//  Created by 宏泰财富 on 2018/11/8.
//  Copyright © 2018 宏泰财富. All rights reserved.
//

import UIKit

@available(iOS 10.0, *)
class NavigationController: UINavigationController ,UIGestureRecognizerDelegate {
    static var onceToken = 0;
    override func loadView() {
        super.loadView()
         if NavigationController.onceToken == 0 {
            NavigationController.onceToken = 1
            setUpNavigationBarThem()
            setUpBarButtonItemTheme()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    func setUpNavigationBarThem(){
        let appearance = UINavigationBar.appearance()

        let textAttrs = NSMutableDictionary()
        textAttrs.setObject( UIColor(displayP3Red: 17/255.0, green: 17/255.0, blue: 17/255.0, alpha: 1.0), forKey: NSAttributedString.Key.foregroundColor as NSCopying)
        textAttrs.setObject(UIFont.systemFont(ofSize: 18), forKey: NSAttributedString.Key.font as NSCopying)
        appearance.titleTextAttributes = textAttrs as? [NSAttributedString.Key : Any]
        
    }
    
    func setUpBarButtonItemTheme(){
        let apperance = UIBarButtonItem.appearance()
        //normal
        let textAttrs = NSMutableDictionary()
        textAttrs.setObject(UIColor(displayP3Red: 17/255.0, green: 17/255.0, blue: 17/255.0, alpha: 1.0), forKey: NSAttributedString.Key.foregroundColor as NSCopying)
        textAttrs.setObject(UIFont.systemFont(ofSize: 16.0), forKey: NSAttributedString.Key.font as NSCopying)
        apperance.setTitleTextAttributes(textAttrs as? [NSAttributedString.Key: Any], for: .normal)
        
        //highlight
        let hightTextAttrs = NSMutableDictionary()
        hightTextAttrs.setObject(UIColor(displayP3Red: 17/255.0, green: 17/255.0, blue: 17/255.0, alpha: 1.0), forKey: NSAttributedString.Key.foregroundColor as NSCopying)
        apperance.setTitleTextAttributes(hightTextAttrs as? [NSAttributedString.Key: Any], for: .highlighted)
        
        //Disable
        let disableTextAttrs = NSMutableDictionary(dictionary: textAttrs)
        disableTextAttrs.setObject(UIColor.lightGray, forKey: NSAttributedString.Key.foregroundColor as NSCopying)
        apperance.setTitleTextAttributes(disableTextAttrs as? [NSAttributedString.Key: Any], for: .disabled)
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0  {
            if viewController.isKind(of: BaseViewController.classForCoder()){
                viewController.hidesBottomBarWhenPushed = true
                let baseVc: BaseViewController = viewController as!BaseViewController
                viewController.navigationItem.leftBarButtonItem = createBackBarButtonItem(self.viewControllers.last?.navigationItem.title   ?? "返回", target: baseVc, sel:#selector(back))
            }else{
                viewController.navigationItem.leftBarButtonItem = createBackBarButtonItem(self.viewControllers.last?.navigationItem.title ?? "返回", target: self, sel: #selector(popViewControllerAnimated))

            }
        }
        super.pushViewController(viewController, animated: animated)
    }

    private func createBackBarButtonItem(_ title: String?, target: Any, sel:Selector) -> UIBarButtonItem{
        
        let btn = UIButton.init(type: .custom)
        btn.contentHorizontalAlignment = .left
        btn.addTarget(target, action: sel, for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.setTitleColor(UIColor.gray, for: .highlighted)
        btn.sizeToFit()
        let h = self.navigationBar.frame.size.height
        let w = max(h, btn.frame.size.width)
        btn.bounds = CGRect(x: 0, y: 0, width: w, height: h)
        return UIBarButtonItem.init(customView: btn)
    }
    
    @objc public func back(){
        
    }
    
    @objc public func popViewControllerAnimated() -> UIViewController{
        if self.viewControllers.count > 2 {
            let willAppearVC = self.viewControllers[self.viewControllers.count - 2]
            if willAppearVC.isKind(of: BaseViewController.classForCoder()){
                let vc: BaseViewController = willAppearVC as! BaseViewController
                vc.navigationBarTintColor? = vc.navigationBarTintColor!
                vc.navigationBarTranslucent? = vc.navigationBarTranslucent!
                vc.navigationBarBackgroungImage? = vc.navigationBarBackgroungImage!
                vc.navigationBarShadowImage? = vc.navigationBarShadowImage!
            }
        }
        return super.popViewController(animated: true)!
    }
    
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.children.count == 1{
            return false
        }
        let lastVc = self.children.last;
        if (lastVc?.isKind(of: BaseViewController.classForCoder()))!{
            let vc: BaseViewController = lastVc as! BaseViewController
            if vc.interactivePopDisabled ?? false {
                return false
            }
        }
        return true
    }
    
   //控制屏幕方向的
    override var shouldAutorotate: Bool{
        return (self.children.last?.shouldAutorotate)!;
    }
 
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return (self.children.last?.supportedInterfaceOrientations)!
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        return (self.children.last?.preferredInterfaceOrientationForPresentation)!
    }
    
    //状态栏设置
    override var prefersStatusBarHidden: Bool{
        return (self.children.last?.prefersStatusBarHidden)!
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return (self.children.last?.preferredStatusBarStyle)!
    }
 
}
